/*
 *  Copright: 2008 Philipp Leitner, Distributed Systems Group,
 *            TU Wien, leitner@infosys.tuwien.ac.at
 *
 *  This file is part of DAIOS.
 *
 *  DAIOS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation.
 * 
 *  DAIOS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with DAIOS.  If not, see
 *  <http://www.gnu.org/licenses/>.
 * 
 */

package at.ac.tuwien.infosys.dsg.daios.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import at.ac.tuwien.infosys.dsg.daios.exceptions.UnsupportedClassException;
import at.ac.tuwien.infosys.dsg.daios.framework.structs.DaiosMessage;
import at.ac.tuwien.infosys.dsg.daios.wsdl.AtomicTypesMapper;
import at.ac.tuwien.infosys.dsg.daios.wsdl.interfaces.IWSDLType;

/**
 * A general Daios-wide utility class. Implements a few conversation
 * helpers and similar.
 * 
 * @author philipp
 *
 */
public class Utility {
	
	/**
	 * Fetch a HTTP document from a URL, and return it as String.
	 * 
	 * @param url The URL where the document to fetch should be
	 * located.
	 * @return The document as String.
	 * @throws IOException
	 */
	public static String getDocumentFromURL(URL url) throws IOException {
		
		BufferedReader reader = new BufferedReader
			(new InputStreamReader(url.openStream()));
		
		String line = null;
		StringBuffer doc = new StringBuffer();
		
		while((line = reader.readLine()) != null) {
			doc.append(line);
		}
			
		return doc.toString();
		
	}
	
	/**
	 * Convert a list of Strings into an array of a certain
	 * type.
	 * 
	 * <p>
	 * 
	 * You have to upcast the resulting Object into the actual
	 * type, i.e.
	 * 
	 * <pre>
	 *   Integer[] ints = (Integer[])convert(l,"int");
	 * </pre>
	 * 
	 * @param l  The list of Strings to convert.
	 * @param arrayType The type of the new array.
	 * @return The array as java.lang.Object
	 * @throws UnsupportedClassException
	 * @throws IOException
	 */
	public static Object convert(List<String> l, String arrayType)
		throws UnsupportedClassException, IOException {
		
		Class arrayclass = AtomicTypesMapper.get(arrayType);
		Object array = Array.newInstance(arrayclass, l.size());
		Object castedVal = null;
		int i = 0;
		for(String s : l) {
			castedVal = AtomicTypesMapper.construct(s,arrayclass);
			Array.set(array,i++,castedVal);
		}
			
		return array;
		
	}
	
	/**
	 * Convert a list of objects into an array of objects.
	 * 
	 * @param l The list of objects.
	 * @return The array containing the same objects.
	 */
	public static Object[] convert(List<Object> l) {
		
		Object[] objects = new Object[l.size()];
		
		Iterator<Object> it = l.iterator();
		int i=0;
		while(it.hasNext())
			objects[i++] = it.next();
		
		return objects;
		
	}
	
	/**
	 * Convert a list of Daios messages to an array of messages.
	 * 
	 * @param l The list of messages.
	 * @return The list of messages as array of messages.
	 */
	public static DaiosMessage[] convert(List<DaiosMessage> l) {
		
		DaiosMessage[] messages = new DaiosMessage[l.size()];
		
		Iterator<DaiosMessage> it = l.iterator();
		int i=0;
		while(it.hasNext())
			messages[i++] = it.next();
		
		return messages;
		
	}
	
	/**
	 * Convert a list of types into an array of types.
	 * 
	 * @param tmpTypes The list of types.
	 * @return The list as array.
	 */	
	public static IWSDLType[] convert(List<IWSDLType> tmpTypes) {
		
		IWSDLType[] typearray = new IWSDLType[tmpTypes.size()];
		int i=0;
		for(IWSDLType t: tmpTypes)
			typearray[i++] = t;
		
		return typearray;
			
	}
	
}
